package com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.impl;

import com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.Translator;
import com.derbysoft.dswitch.dto.hotel.avail.HotelAvailCriteriaDTO;
import com.derbysoft.dswitch.dto.hotel.avail.HotelAvailRQ;
import com.derbysoft.dswitch.dto.hotel.avail.RoomStayCandidateDTO;
import com.derbysoft.dswitch.dto.hotel.reservation.HotelReservationRQ;
import com.derbysoft.dswitch.dto.hotel.reservation.ReservationRoomStayDTO;
import com.derbysoft.dswitch.remote.hotel.dto.HotelAvailRequest;
import com.derbysoft.dswitch.remote.hotel.dto.HotelReservationRequest;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by: jason
 * Date: 2012-05-14
 */
@Component("hotelPreviewRQTranslator")
public class HotelPreviewRQTranslatorImpl extends BaseTranslator
        implements Translator<HotelReservationRequest, HotelAvailRequest, Void> {
    @Override
    public HotelAvailRequest translate(HotelReservationRequest reservationRequest, Void aVoid) {
        HotelAvailRequest hotelAvailRequest = new HotelAvailRequest();
        hotelAvailRequest.setHeader(reservationRequest.getHeader());
        hotelAvailRequest.setHotelAvailRQ(createAvailRQ(reservationRequest));
        return hotelAvailRequest;
    }

    private HotelAvailRQ createAvailRQ(HotelReservationRequest reservationRequest) {
        HotelReservationRQ hotelReservationRQ = reservationRequest.getHotelReservationRQ();
        ReservationRoomStayDTO reservationRoomStayDTO = hotelReservationRQ.getHotelReservation().getRoomStaysList().get(0);

        HotelAvailRQ hotelAvailRQ = new HotelAvailRQ();
        HotelAvailCriteriaDTO availCriteria = new HotelAvailCriteriaDTO();

        availCriteria.setStayDateRange(reservationRoomStayDTO.getStayDateRange());
        List<String> hotelCodes = new ArrayList<String>();
        hotelCodes.add(reservationRequest.getHotelReservationRQ().getHotelReservation().getHotelCode());
        availCriteria.setHotelCodesList(hotelCodes);
        availCriteria.setRoomStayCandidate(createRoomStayCandidate(reservationRoomStayDTO));
        hotelAvailRQ.setAvailCriteria(availCriteria);
        return hotelAvailRQ;
    }

    private RoomStayCandidateDTO createRoomStayCandidate(ReservationRoomStayDTO reservationRoomStayDTO) {
        RoomStayCandidateDTO roomStayCandidateDTO = new RoomStayCandidateDTO();
        roomStayCandidateDTO.setAdultCount(reservationRoomStayDTO.getAdultCount());
        roomStayCandidateDTO.setChildCount(reservationRoomStayDTO.getChildCount());
        roomStayCandidateDTO.setNumberOfUnits(reservationRoomStayDTO.getNumberOfUnits());
        return roomStayCandidateDTO;
    }
}
